﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;
using LeadVinaglue.Models;

namespace LeadVinaglue.Repository
{
    public partial class EmailTemplateRepository
    {
        LeadVinaglueEntities entities = new LeadVinaglueEntities();

        public List<EmailTemplateModel> GetAll()
        {
            List<EmailTemplateModel> list = new List<EmailTemplateModel>();

            var items = (from c in entities.EmailTemplates
                    orderby c.DateCreated descending
                    select c).ToList();

            foreach (var item in items)
            {
                EmailTemplateModel model = new EmailTemplateModel();

                model.ID = item.ID;
                model.EmailType = item.EmailType;
                model.SubjectLine = item.SubjectLine;
                model.FromAddress = item.FromAddress;
                model.CC = item.CC;
                model.DateCreated = Convert.ToDateTime(item.DateCreated).ToString("dd-MM-yyyy hh:mm tt");
                model.Active = item.Active;

                list.Add(model);
            }

            return list;
        }

        public EmailTemplate GetSingle(int id)
        {
            return (from c in entities.EmailTemplates where c.ID == id select c).FirstOrDefault();
        }

        public EmailTemplateModel GetModelItem(int id)
        {
            EmailTemplateModel model = new EmailTemplateModel();
            var email = (from _object in entities.EmailTemplates where _object.ID == id select _object).FirstOrDefault();

            if (email != null)
            {
                model.ID = email.ID;
                model.EmailType = email.EmailType;
                model.SubjectLine = email.SubjectLine;
                model.FromAddress = email.FromAddress;
                model.CC = email.CC;
                model.Body = email.Body;
                model.Active = email.Active;
            }

            return model;
        }


        public EmailTemplate GetSingle(string emailType)
        {
            return (from _object in entities.EmailTemplates where _object.EmailType == emailType select _object).FirstOrDefault();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] EmailTemplate _object)
        {
            _object.DateCreated = System.DateTime.Now;
             entities.AddToEmailTemplates(_object);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.EmailTemplates where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }

        public void Update(EmailTemplate _object)
        {
            entities.SaveChanges();
        }

        public void Save([Bind(Exclude = "ID")] EmailTemplate _object)
        {
            _object.EntityKey = (from EmailTemplate ac in entities.EmailTemplates where ac.ID == _object.ID select ac).FirstOrDefault().EntityKey;
            _object.DateCreated = System.DateTime.Now;
            entities.ApplyPropertyChanges(_object.EntityKey.EntitySetName, _object);
            entities.SaveChanges();
        }
    }
}
